package gogogo.liwushuo.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.ButterKnife;
import butterknife.InjectView;
import gogogo.liwushuo.R;
import gogogo.liwushuo.bean.GiftBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.util.OKHttpUtils;

public class BannerFirstActivity extends AppCompatActivity {

    @InjectView(R.id.back)
    ImageView back;
    @InjectView(R.id.tv_toolbar)
    TextView tvToolbar;
    @InjectView(R.id.iv_daily_gift)
    ImageView ivDailyGift;
    @InjectView(R.id.tv_daily_gift)
    TextView tvDailyGift;
    @InjectView(R.id.gift_price)
    TextView giftPrice;
    @InjectView(R.id.tv_membernum)
    TextView tvMembernum;
    @InjectView(R.id.iv_gift_result)
    ImageView ivGiftResult;
    @InjectView(R.id.tv_group1)
    LinearLayout tvGroup1;
    @InjectView(R.id.tv_group2)
    LinearLayout tvGroup2;
    @InjectView(R.id.tv_time)
    TextView tvTime;


    private OKHttpUtils okHttpUtils;
    private Context mContext;
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner_first);
        ButterKnife.inject(this);
        initData();
        getJsonData();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        okHttpUtils = OKHttpUtils.newInstance(this);
        mContext = this;
        mHandler =new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 1){
                    long l = 24 * 60 * 60 * 1000;
                    long time = System.currentTimeMillis();
//                    Log.i("mtag", "onResponse: " + time);
                    long leftTime = (l - time % l);
                    Date date = new Date(leftTime);
                    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                    String s = format.format(date);
                    tvTime.setText(s);
                    mHandler.sendEmptyMessageDelayed(1,1000);
                }
                return false;
            }
        });
    }

    private void getJsonData() {
        okHttpUtils.getAsyncMainData(Resource.URL_GIFT, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                GiftBean giftBean = new Gson().fromJson(result, GiftBean.class);
                Picasso.with(mContext)
//                        1469242731
                        .load(giftBean.getData().getItem().getCover_image_url())
                        .into(ivDailyGift);
                tvDailyGift.setText(giftBean.getData().getItem().getTitle());
                tvMembernum.setText(giftBean.getData().getVisitors() + "");
                tvGroup1.setVisibility(View.VISIBLE);
                tvGroup2.setVisibility(View.VISIBLE);
//                long hour = leftTime / 3600000;
//                long minute = leftTime % 3600000 / 60000;
//                long seconds = leftTime % 3600000 % 60000 /1000;
//                String s = hour + ":" + minute + ":" + seconds;
                giftPrice.setText("￥" + giftBean.getData().getItem().getFixed_price() + "");
                ivGiftResult.setImageResource(R.mipmap.no_award_default_bg);
                mHandler.sendEmptyMessageDelayed(1,1000);
            }
        });
    }


}
